package com.shop.handler;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginHandler implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("-----------进来了");
//        用户登录了，才可以进入否则不可以进入
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("admin");
        if (obj != null) {
            return true;
        } else {
            //说明没有登录
            request.setAttribute("msg","对不起，你没有权限，请先登录！");
            request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
//            return false;
//            测试阶段我全部设置为true
            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
